<template>
	<view>
		<view class="container" v-if="isShow">
			<custom-header title="常见问题"></custom-header>

			<!-- 列表 -->
			<scroll-view scroll-y enable-back-to-top @scrolltolower="loadMore" @refresherrefresh="onRefresh"
				refresher-enabled :refresher-triggered="isRefreshing" style="height: 100%; padding: 28rpx 68rpx;">
				<block v-for="(item, index) in questionList" :key="index">
					<view style="margin-bottom: 28rpx;" @click="toDetail(item.id)">
						<view class="list-item">
							<view class="item-num">
								<view>{{index+1}}</view>
							</view>

							<view class="item-title">
								<view>{{ item.title }}</view>
							</view>

							<image src="../../static/images/arrow_right.png" style="width: 12rpx; height: 22rpx;">
							</image>
						</view>
						<view style="height: 2rpx; background-color: rgba(0, 0, 0, 0.09);"></view>
					</view>
				</block>
				<view v-if="loadingMore" class="loading-more">加载中...</view>
				<view v-if="noMoreData" class="no-more-data">已经全部加载完毕</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				questionList: [],
				page: 1,
				pageSize: 10,
				isRefreshing: false, //正在刷新
				loadingMore: false, //加载中
				noMoreData: false, //没有更多
				isEnd: false, // 是否全部加载完成
				total: 0, //总数据量
				isShow: uni.getStorageSync("isShow"),
			};
		},
		onLoad() {
			this.fetchQuestionList(0)
		},
		methods: {
			onRefresh() {
				this.isRefreshing = true;
				this.resetPagination();
				this.fetchQuestionList(1);
			},
			// 清空状态
			resetPagination() {
				this.page = 1;
				this.announcementList = [];
				this.loadingMore = false;
				this.noMoreData = false;
			},
			loadMore() {
				if (!this.loadingMore && !this.noMoreData) {
					this.loadingMore = true;
					this.page += 1;
					this.fetchQuestionList(2)
				}
			},
			async fetchQuestionList(states) {
				const res = await this.$http({
					method: 'GET',
					url: '/article/question/list',
					data: {
						pageSize: this.pageSize,
						pageNun: this.page,
					}
				});

				if (res) {
					//状态修改
					if (states == 1) {
						this.isRefreshing = false
					} else if (states == 2) {
						this.loadingMore = false;
					}

					// 首次加载
					if (this.page === 1) {
						this.questionList = res.rows
						this.total = res.total
					} else {
						// 追加数据
						this.questionList = [...this.questionList, ...res.rows]
					}

					// 判断是否加载完成
					this.isEnd = (this.page * this.pageSize) >= res.total

					// 翻页或加载到最后了
					if (!this.isEnd) {
						this.page++;
					} else {
						this.noMoreData = true;
					}
				} else {
					if (states == 1) {
						this.isRefreshing = false
					} else if (states == 2) {
						this.loadingMore = false;
					}
				}
			},
			// 跳转到详情
			toDetail(id) {
				uni.navigateTo({
					url: `/pages/detail/detail?id=${id}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		height: 100vh;
	}

	.list-item {
		display: flex;
		align-items: center;
		padding-bottom: 28rpx;

		.item-num {
			height: 32rpx;
			width: 32rpx;
			background-color: #4D7EF4;
			color: white;
			font-size: 20rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			border-radius: 16rpx;
		}

		.item-title {
			margin-left: 10rpx;
			flex: 1;
			font-size: 28rpx;
			color: black;
		}
	}

	// 加载更多
	.loading-more,
	.no-more-data {
		text-align: center;
		padding: 10px;
	}
</style>